package com.simulate2.demo.service;

import com.simulate2.demo.dao.UserRepository;
import com.simulate2.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    //随机生成新用户，存到数据库中。

    public void produceUser(int n, int date) {

        List<User> userList = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            int name = (int) (Math.random() * 10000 + 1);
            //生成1-10之间的整数，乘100即100-1000的整百
            int base = rand.nextInt(11);
            //int base=(int)(Math.random()*10+1);
            int allCredit = base * 100;
            //int activeCredit=(int)(Math.random()*base+1)*100;
            int activeCredit = rand.nextInt(base + 1) * 100;

            User user = new User(0, "张" + name, allCredit, activeCredit, date);
            userList.add(user);

        }
        userRepository.saveAll(userList);
    }


}
